package com.promis.util;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

import java.util.HashMap;

/**
 * Created by jidanbo on 2018/3/14.
 */

public class VideoUtils {

    public static int getDuration(String path) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(path);
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        retriever.release();

        if (StringUtils.isNullOrEmpty(time)) return 0;
        else return Integer.parseInt(time);
    }

//    public static Bitmap getThumbnail(String url, int width, int height) {
//        Bitmap bitmap = null;
//        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
//        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
//            bitmap = ThumbnailUtils.extractThumbnail(getThumbnail(url), width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
//        }
//        return bitmap;
//    }

    public static Bitmap getThumbnailByLocalFile(String filePath) {

        Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
        if (bitmap == null) return null;
        Log.i("Thumbnail", "Width :" + bitmap.getWidth() + " | Height:" + bitmap.getHeight());
        return ThumbnailUtils.extractThumbnail(bitmap, bitmap.getWidth(), bitmap.getHeight(), ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    }

    public static Bitmap getThumbnailByUrl(String url) {

        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                retriever.setDataSource(url, new HashMap<String, String>());
            } else {
                retriever.setDataSource(url);
            }
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // Ignore failures while cleaning up.
            }
        }
        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, bitmap.getWidth(), bitmap.getHeight(),
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

    public static int[] getRect(String url) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(url);
        String width = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
        String height = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        retriever.release();

        int[] demin = new int[2];
        demin[0] = Integer.parseInt(width);
        demin[1] = Integer.parseInt(height);
        return demin;
    }

    public static int getWidth(String url) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(url);
        String width = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
        retriever.release();

        return Integer.parseInt(width);
    }

    public static int getHeight(String url) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(url);
        String height = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        retriever.release();

        return Integer.parseInt(height);
    }
}
